

package com.hazelcast.config;

import com.hazelcast.internal.services.ServiceConfigurationParser;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import static com.hazelcast.internal.config.DomConfigHelper.childElements;
import static com.hazelcast.internal.config.DomConfigHelper.cleanNodeName;
import static org.ops4j.util.xml.XmlUtils.getTextContent;

public class MyServiceConfigParser extends AbstractXmlConfigHelper implements ServiceConfigurationParser<MyServiceConfig> {

    @Override
    public MyServiceConfig parse(Element element) {
        MyServiceConfig config = new MyServiceConfig();
        for (Node configNode : childElements(element)) {
            if ("my-service".equals(cleanNodeName(configNode))) {
                for (Node node : childElements(configNode)) {
                    final String name = cleanNodeName(node);
                    if ("string-prop".equals(name)) {
                        config.stringProp = getTextContent(node);
                    } else
                        if ("int-prop".equals(name)) {
                            String value = getTextContent(node);
                            config.intProp = Integer.parseInt(value);
                        } else
                            if ("bool-prop".equals(name)) {
                                config.boolProp = Boolean.parseBoolean(getTextContent(node));
                            } else
                                if ("complex-prop".equals(name)) {
                                    Node attribute = node.getAttributes().getNamedItem("an-attribute");
                                    config.nestedAttribute = attribute.getTextContent();
                                    for (Node nestedNode : childElements(node)) {
                                        final String nestedNodeName = cleanNodeName(nestedNode);
                                        if ("nested-prop".equals(nestedNodeName)) {
                                            config.nestedStringProp = getTextContent(nestedNode);
                                        }
                                    }
                                }
                }
            }
        }
        return config;
    }

}
